﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using IndoReputation.Models.BusinessLogic;
using Telerik.Web.Mvc;
using IndoReputation.Views.ViewModels;
using IndoReputation.Models.BusinessObject;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class NewsCategoryController : BaseController
    {
        //
        // GET: /NewsCategory/

        public ActionResult Index()
        {
            ViewData["ajax"] = true;
            ViewData["scrolling"] = true;
            ViewData["paging"] = true;
            ViewData["filtering"] = true;
            ViewData["grouping"] = true;
            ViewData["sorting"] = true;
            ViewData["showFooter"] = true;
            return View(NewsCategoryBOL.GetAllNewsCategoryJoin());
        }

        [GridAction]
        public ActionResult AjaxIndex()
        {
            return View(new GridModel(NewsCategoryBOL.GetAllNewsCategoryJoin()));
        }

        public void GetAllNewsCategory(int? parentId, NewsCategory parentNode, ref List<NewsCategory> afterBind, List<NewsCategory> source)
        {
            var filteredItems = source.Where(item => item.ParentID == parentId);
            foreach (var i in filteredItems)
            {
                var temp = new NewsCategory
                {
                    ChildLocations = i.ChildLocations,
                    CreateAt = i.CreateAt,
                    CreateBy = i.CreateBy,
                    Description = i.Description,
                    ID = i.ID,
                    Name = i.Name,
                    ParentID = i.ParentID,
                    UpdateAt = i.UpdateAt,
                    UpdateBy = i.UpdateBy
                };
                if (parentNode == null)
                {                    
                    afterBind.Add(temp);                    
                }
                else
                {
                    parentNode.ChildLocations.Add(temp);                    
                }
                var n = temp;
                GetAllNewsCategory(i.ID, n, ref afterBind, source);
            }
        }

        [HttpGet]
        public ActionResult Add()
        {
            var parent = NewsCategoryBOL.GetAllNewsCategory();
            var listNc = new List<NewsCategory>
                             {
                                 new NewsCategory
                                     {
                                         ID = 0,
                                         Name = "No Parent"
                                     }
                             };
            GetAllNewsCategory(null, null, ref listNc, parent);
            var model = new NewsCategoryViewModel
            {
                ListNewsCategory = listNc                
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Add(NewsCategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.ParentID == 0)
                    model.ParentID = null;
                var newsCat = new NewsCategory
                {
                    Description = model.Description,
                    Name = model.Name,
                    ParentID = model.ParentID
                };
                NewsCategoryBOL.Insert(newsCat);
            }
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Edit(int newsCategoryID)
        {
            var parent = NewsCategoryBOL.GetAllNewsCategory();
            var listNC = new List<NewsCategory>
                             {
                                 new NewsCategory
                                     {
                                         ID = 0,
                                         Name = "No Parent"
                                     }
                             };
            GetAllNewsCategory(null, null, ref listNC, parent);

            var newsC = NewsCategoryBOL.GetNewsCategoryById(newsCategoryID);
            
            var model = new NewsCategoryViewModel
            {
                CreateAt = newsC.CreateAt,
                CreateBy = newsC.CreateBy,
                Description = newsC.Description,
                ID = newsC.ID,
                Name = newsC.Name,
                ParentID = newsC.ParentID ?? 0,
                ListNewsCategory = listNC
            };            
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(NewsCategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.ParentID == 0)
                    model.ParentID = null;
                var newsCat = new NewsCategory
                {
                    CreateAt = model.CreateAt,
                    CreateBy = model.CreateBy,
                    ID = model.ID,
                    Description = model.Description,
                    Name = model.Name,
                    ParentID = model.ParentID
                };
                NewsCategoryBOL.Update(newsCat);
            }
            //return View(model);
            return RedirectToAction("Index");
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult AjaxDelete(int id)
        {
            //Find a customer with ProductID equal to the id action parameter            
            if (id > 0)
            {
                //Delete the record
                if (!NewsCategoryBOL.Delete(id))
                {
                    ModelState.AddModelError(String.Empty, @"This News Category have New(s) or child NewsCategory(s). Please check again!");
                }
            }
            //Rebind the grid
            return View(new GridModel(NewsCategoryBOL.GetAllNewsCategoryJoin()));
        }

    }
}
